<script lang="ts" setup name="XtxCarousel">
import { BannerItem } from '@/types/data'
import { onUnmounted, PropType, ref } from 'vue'

// vue3接收父传子 没有类型提示 可以配合 PropType
// const props = defineProps({
//   list: {
//     type: Array as PropType<BannerItem[]>,
//     required: true
//   },
//   interval: {
//     type: Number,
//     default: 5000
//   },
//   autoplay: {
//     type: Boolean,
//     default: false
//   },
//   isShowDots: {
//     type: Boolean,
//     default: false
//   }
// })

// Ts接收父传子 有类型提示 ts解构出来的数据是没有响应式的
// 因此需要在vite.config.js中进行配置
const {
  list,
  interval = 5000,
  autoplay = false,
  isShowDots = false
} = defineProps<{
  list: BannerItem[]
  interval?: number
  autoplay?: boolean
  isShowDots?: boolean
}>()

// 默认选中索引
const ActiveIndex = ref(0)

// 上一张
const prev = () => {
  ActiveIndex.value--
  if (ActiveIndex.value < 0) {
    ActiveIndex.value = list.length - 1
  }
}

// 下一张
const next = () => {
  ActiveIndex.value++
  if (ActiveIndex.value > list.length - 1) {
    ActiveIndex.value = 0
  }
}

// 自动轮播
let timerId = -1

// 开启定时器
const start = () => {
  if (!autoplay) return
  clearInterval(timerId)
  timerId = window.setInterval(() => {
    next()
  }, interval)
}
start()

// 关闭定时器
const stop = () => {
  clearInterval(timerId)
}

// 组件销毁时移除事件
onUnmounted(() => {
  stop()
})
</script>

<template>
  <div class="xtx-carousel" @mouseenter="stop" @mouseleave="start">
    <!-- 轮播图图片 -->
    <ul class="carousel-body">
      <li
        class="carousel-item"
        :class="{ fade: ActiveIndex === index }"
        v-for="(item, index) in list"
        :key="item.id"
      >
        <RouterLink to="/">
          <img :src="item.imgUrl" alt="" />
        </RouterLink>
      </li>
    </ul>

    <!-- 左右箭头 -->
    <a href="javascript:;" class="carousel-btn prev" @click="prev"
      ><i class="iconfont icon-angle-left"></i
    ></a>
    <a href="javascript:;" class="carousel-btn next" @click="next"
      ><i class="iconfont icon-angle-right"></i
    ></a>

    <!-- 小圆点 -->
    <div class="carousel-indicator" v-if="isShowDots">
      <span
        :class="{ active: ActiveIndex === index }"
        v-for="(item, index) in list"
        :key="item.id"
        @mouseenter="ActiveIndex = index"
      ></span>
    </div>
  </div>
</template>

<style scoped lang="less">
.xtx-carousel {
  width: 100%;
  height: 100%;
  min-width: 300px;
  min-height: 150px;
  position: relative;
  .carousel {
    &-body {
      width: 100%;
      height: 100%;
    }
    &-item {
      width: 100%;
      height: 100%;
      position: absolute;
      left: 0;
      top: 0;
      opacity: 0;
      transition: opacity 0.5s linear;
      &.fade {
        opacity: 1;
        z-index: 1;
      }
      img {
        width: 100%;
        height: 100%;
      }
    }
    &-indicator {
      position: absolute;
      left: 0;
      bottom: 20px;
      z-index: 2;
      width: 100%;
      text-align: center;
      span {
        display: inline-block;
        width: 12px;
        height: 12px;
        background: rgba(0, 0, 0, 0.2);
        border-radius: 50%;
        cursor: pointer;
        ~ span {
          margin-left: 12px;
        }
        &.active {
          background: #fff;
        }
      }
    }
    &-btn {
      width: 44px;
      height: 44px;
      background: rgba(0, 0, 0, 0.2);
      color: #fff;
      border-radius: 50%;
      position: absolute;
      top: 228px;
      z-index: 2;
      text-align: center;
      line-height: 44px;
      opacity: 0;
      transition: all 0.5s;
      &.prev {
        left: 20px;
      }
      &.next {
        right: 20px;
      }
    }
  }
  &:hover {
    .carousel-btn {
      opacity: 1;
    }
  }
}
</style>
